package com.graduation.cdm.funactivity.dialog;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.graduation.cdm.funactivity.MyApplication;
import com.graduation.cdm.funactivity.R;
import com.graduation.cdm.funactivity.adapter.MenuDialogAdapter;
import com.graduation.cdm.funactivity.adapter.MyPagerAdapter;
import com.graduation.cdm.funactivity.bean.DomainBean;
import com.graduation.cdm.funactivity.util.MenuDataManager;
import com.graduation.cdm.funactivity.util.ownview.MyViewPager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.graduation.cdm.funactivity.util.GlobalConstant.preUrl;

/**
 * 三级菜单列表
 * Created by LaiYingtang on 2016/5/25.
 */
public class ThreeMenuDialog extends SecondMenuDialog{

    private  int mWidth;    //宽度
    private MyViewPager mViewPager; //滑动viewPager
    private LinearLayout mRootView;    //需要显示的layout
    private View view1,view2;    //三个菜单级view
    private ListView mListView1,mListView2;  //每个菜单列表都是一个listView
    private MenuDialogAdapter mListView1Adapter, mListView2Adapter; //列表显示数据必须要的adapter
    private List<View> views = new ArrayList<View>(); //数据集合
    public MenuDataManager mDictDataManager = MenuDataManager.getInstance(); //全部数据
    private MenuItemClickListener menuItemClickListener;   //接口，点击监听

    private List<String> lableList = new ArrayList<>();
    private List<DomainBean.Domain> domainList = new ArrayList<>();
    private List<DomainBean.Domain> provinceList = new ArrayList<>();
    private List<DomainBean.Domain> cityList = new ArrayList<>();
    private List<Map<String,String>> provinceAllList = new ArrayList<>();
    private ArrayAdapter<String> adapter;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    mListView1Adapter = new MenuDialogAdapter(mContext, domainList);
                    mListView1Adapter.setSelectedBackgroundResource(R.drawable.select_white);//选中时的背景
                    mListView1Adapter.setHasDivider(false);
                    mListView1Adapter.setNormalBackgroundResource(R.color.menudialog_bg_gray);//未选中
                    mListView1.setAdapter(mListView1Adapter);
                    break;
                case 2:
                    if (mListView2Adapter == null) {
                        mListView2Adapter = new MenuDialogAdapter(mContext, domainList);
                        mListView2Adapter.setNormalBackgroundResource(R.color.white);
                        mListView2.setAdapter(mListView2Adapter);
                    } else {
                        mListView2Adapter.setData(domainList);
                        mListView2Adapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };

    public ThreeMenuDialog(Context context) {
        super(context);
        mWidth = mContext.getResources().getDisplayMetrics().widthPixels;//获取屏幕参数
        mContentView = LayoutInflater.from(context).inflate(R.layout.three_menu_dialog,null);
        //初始化控件及对控件操作
        initViews();
        setTitle("地域选择");//设置title
    }

    private void initViews() {
        mRootView = (LinearLayout) findViewById(R.id.rootview);
        mViewPager = (MyViewPager) findViewById(R.id.viewpager);
        mViewPager.setOffscreenPageLimit(2);//显示2页

        //为view加载layout,由于三个级的菜单都是只有一个listView，这里就只xie一个了
        LayoutInflater inflater = LayoutInflater.from(mContext);
        view1 = inflater.inflate(R.layout.pager_number, null);
        view2 = inflater.inflate(R.layout.pager_number, null);

        //获取id
        mListView1 = (ListView) view1.findViewById(R.id.listview);
        mListView2 = (ListView) view2.findViewById(R.id.listview);

        //获取列表数据了
        getDomain("Province","0");

        views.add(view1);
        views.add(view2);//当前是第三级菜单，所以前面已经存在第一，第二菜单了

        //关联
        mViewPager.setAdapter(new MyPagerAdapter(views));
        //触屏监听
        mRootView.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return mViewPager.dispatchTouchEvent(event);
            }
        });

        //view1的listView的点击事件
        //点击事件
        mListView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListView1Adapter != null)
                    mListView1Adapter.notifyDataSetChanged();
                if (mListView2Adapter != null)
                    mListView2Adapter.notifyDataSetChanged();

                DomainBean.Domain domain = ((DomainBean.Domain) parent.getItemAtPosition(position));
                if (domain.domainText.equals("不限")) {//不限
                    if (mListView2Adapter != null) {
                        mListView2Adapter.setData(new ArrayList<DomainBean.Domain>());
                        mListView2Adapter.notifyDataSetChanged();//刷新
                    }
                    setDictItemClickListener(domain);
                } else {
                    getDomain("City", provinceList.get(position).id+"");

                }
            }
        });


        //view2的listView点击
        mListView2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                DomainBean.Domain domain = ((DomainBean.Domain) parent.getItemAtPosition(position));
                setDictItemClickListener(domain);//选中点击的子菜单，去设置titleName
            }
        });

    }

    private void setDictItemClickListener(DomainBean.Domain domain) {
        if (menuItemClickListener != null) {
            menuItemClickListener.onMenuItemClick(domain);
        }
        dismiss();
    }

    public final void setonItemClickListener(MenuItemClickListener listener) {
        menuItemClickListener = listener;
    }

    public interface MenuItemClickListener {
        public void onMenuItemClick(DomainBean.Domain domain);
    }

    private void getDomain(final String namespace, final String parentId){
        String url=preUrl+"domain/getDomain";
        StringRequest stringRequest=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                System.out.println("........"+s.trim());
                Gson gson = new Gson();
                DomainBean domainBean = gson.fromJson(s,DomainBean.class);
                if (namespace.equals("City")){
                    domainList.clear();
                    cityList.clear();
                }
                domainList.addAll(domainBean.domains);
                if (namespace.equals("Province")) {
                    DomainBean domainBean1 = new DomainBean();
                    DomainBean.Domain mDomain = domainBean.new Domain();
                    mDomain.domainText = "不限";
                    provinceList.add(mDomain);
                    provinceList.addAll(domainList);
                    mListView1Adapter = new MenuDialogAdapter(mContext, provinceList);
                    mListView1Adapter.setSelectedBackgroundResource(R.drawable.select_white);//选中时的背景
                    mListView1Adapter.setHasDivider(false);
                    mListView1Adapter.setNormalBackgroundResource(R.color.menudialog_bg_gray);//未选中
                    mListView1.setAdapter(mListView1Adapter);
                }
                if (namespace.equals("City")){
                    cityList.addAll(domainList);
                    if (mListView2Adapter == null) {
                        mListView2Adapter = new MenuDialogAdapter(mContext, cityList);
                        mListView2Adapter.setNormalBackgroundResource(R.color.white);
                        mListView2.setAdapter(mListView2Adapter);
                    } else {
                        mListView2Adapter.setData(cityList);
                        mListView2Adapter.notifyDataSetChanged();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                System.out.println("==="+volleyError.toString());
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("namespace",namespace);
                map.put("parentId",parentId);
                return map;
            }
        };
        MyApplication.getRequestQueue().add(stringRequest);
    }
}
